from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QFrame
from PyQt5.QtWebEngineWidgets import QWebEngineView
import sys
#from datetime import datetime
from influxdb_client import InfluxDBClient, Point, WritePrecision
#from influxdb_client.client.write_api import SYNCHRONOUS
#from pyecharts.charts import Bar
from pyecharts.charts import Line
from pyecharts.charts import Page
#选择
from pyecharts import options as opts
class Stacked(QWidget):
    def __init__(self):
        super(Stacked, self).__init__()
        self.initUI()

        self.set_up()
        self.mainLayout()
    def initUI(self):
        self.setGeometry(400, 400, 800, 600)
        self.setWindowTitle("数据总表")

    def mainLayout(self):
        self.mainhboxLayout = QHBoxLayout(self)
        self.frame = QFrame(self)
        self.mainhboxLayout.addWidget(self.frame)
        self.hboxLayout = QHBoxLayout(self.frame)
        self.myHtml = QWebEngineView()
        url = "http://www.baidu.com"
        # 打开本地html文件
        print("前面")
        self.myHtml.load(QUrl('file:///' + 'myecharts.html'))
        print("后面")
        self.hboxLayout.addWidget(self.myHtml)
        self.setLayout(self.mainhboxLayout)

    def set_up(self):
        # You can generate a Token from the "Tokens Tab" in the UI 连接数据库
        token = "9n7h1Da3gpipdxmUBRF41fqPV6WrfqIdTakVmZt6fPoe8cPcFDHEN_IVHLxokE2vn6H39oK6g1EMIlL7l86LCQ=="
        org = "ustb"
        bucket = "nfca_db"
        client = InfluxDBClient(url="http://202.204.54.23:8086", token=token)

        #进料浓度
        time1 = []
        value1 = []

        # 筛选gms_monitor表中，点位 "5" 的最近7天数据
        query = f'from(bucket: "{bucket}") \
        |> range(start: -7d) \
        |> filter(fn: (r) => \
            r._measurement == "gms_monitor" and\
            r._field == "Monitoring_value" \
        )\
        |> filter(fn: (r) => r.point_id == "5")'

        tables = client.query_api().query(query, org=org)

        for table in tables:
            for record in table:
                time1.append(record.get_time())
                value1.append(record.get_value())

        len(time1)
        line1 = Line(init_opts=opts.InitOpts(width='1720px', height='620px'))
        line1.add_xaxis(time1)
        line1.add_yaxis(" ", value1)
        line1.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        line1.set_global_opts(title_opts=opts.TitleOpts(title="进料浓度"), datazoom_opts=opts.DataZoomOpts(is_show=True))
        line1.set_colors("LightSeaGreen")

        # 进料流量
        time2 = []
        value2 = []

        # 筛选gms_monitor表中，点位 "11" 的最近7天数据
        query = f'from(bucket: "{bucket}") \
        |> range(start: -7d) \
        |> filter(fn: (r) => \
            r._measurement == "gms_monitor" and\
            r._field == "Monitoring_value" \
        )\
        |> filter(fn: (r) => r.point_id == "11")'

        tables = client.query_api().query(query, org=org)

        for table in tables:
            for record in table:
                time2.append(record.get_time())
                value2.append(record.get_value())

        line2 = Line(init_opts=opts.InitOpts(width='1720px', height='620px'))
        line2.add_xaxis(time2)
        line2.add_yaxis("", value2)
        line2.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        line2.set_global_opts(title_opts=opts.TitleOpts(title="进料流量"), datazoom_opts=opts.DataZoomOpts(is_show=True))
        line2.set_colors("LightSeaGreen")

        # 底流浓度
        time3 = []
        value3 = []

        # 筛选gms_monitor表中，点位 "7" 的最近7天数据
        query = f'from(bucket: "{bucket}") \
        |> range(start: -7d) \
        |> filter(fn: (r) => \
            r._measurement == "gms_monitor" and\
            r._field == "Monitoring_value" \
        )\
        |> filter(fn: (r) => r.point_id == "7")'

        tables = client.query_api().query(query, org=org)

        for table in tables:
            for record in table:
                time3.append(record.get_time())
                value3.append(record.get_value())

        line3 = Line(init_opts=opts.InitOpts(width='1720px', height='620px'))
        line3.add_xaxis(time3)
        line3.add_yaxis("", value3)
        line3.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        line3.set_global_opts(title_opts=opts.TitleOpts(title="底流浓度"), datazoom_opts=opts.DataZoomOpts(is_show=True))
        line3.set_colors("LightSeaGreen")

        # 底流流量
        time4 = []
        value4 = []

        # 筛选gms_monitor表中，点位 "17" 的最近7天数据
        query = f'from(bucket: "{bucket}") \
        |> range(start: -7d) \
        |> filter(fn: (r) => \
            r._measurement == "gms_monitor" and\
            r._field == "Monitoring_value" \
        )\
        |> filter(fn: (r) => r.point_id == "17")'

        tables = client.query_api().query(query, org=org)

        for table in tables:
            for record in table:
                time4.append(record.get_time())
                value4.append(record.get_value())

        line4 = Line(init_opts=opts.InitOpts(width='1720px', height='620px'))
        line4.add_xaxis(time4)
        line4.add_yaxis("", value4)
        line4.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        line4.set_global_opts(title_opts=opts.TitleOpts(title="底流流量"), datazoom_opts=opts.DataZoomOpts(is_show=True))
        line4.set_colors("LightSeaGreen")

        # 絮凝剂流量
        time5 = []
        value5 = []

        # 筛选gms_monitor表中，点位 "55" 的最近7天数据
        query = f'from(bucket: "{bucket}") \
        |> range(start: -7d) \
        |> filter(fn: (r) => \
            r._measurement == "gms_monitor" and\
            r._field == "Monitoring_value" \
        )\
        |> filter(fn: (r) => r.point_id == "55")'

        tables = client.query_api().query(query, org=org)

        for table in tables:
            for record in table:
                time5.append(record.get_time())
                value5.append(record.get_value())


        line5 = Line(init_opts=opts.InitOpts(width='1720px', height='620px'))
        line5.add_xaxis(time5)
        line5.add_yaxis("", value5)
        line5.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        line5.set_global_opts(title_opts=opts.TitleOpts(title="絮凝剂流量"), datazoom_opts=opts.DataZoomOpts(is_show=True))
        line5.set_colors("LightSeaGreen")

        # 显示到一张图中
        page = (Page().add(line1, line2, line3, line4, line5).render("myecharts.html"))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Stacked()
    ex.show()
    sys.exit(app.exec_())